package com.webber.demo;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
 * 
 * <p>Title: TransmissionServlet.java</p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>ProjectName: uploadFile</p>
 * @Package com.webber.demo 
 * @author JobsZhang
 * @date 2017年10月19日 上午9:41:45
 * @version uploadFile 1.0
 */
public class TransmissionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String serverPath = "E:/test/uploadFile";

	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		
		File f =  new File(serverPath);
		if(!f.exists()) {
			f.mkdirs();
		}
		// 1.配置缓存
		DiskFileItemFactory factory = new DiskFileItemFactory(5 * 1024 * 1024,new File(serverPath));
		// 2.创建ServleFileUpload对象
		ServletFileUpload sfu = new ServletFileUpload(factory);
		// 解决文件名称中文问题
		sfu.setHeaderEncoding("utf-8");
		// 3.解析
		try {
			List<FileItem> list = sfu.parseRequest(request);
			String chunks = null;
			String chunk = null;
			String fileMd5 = null;
			// 解析所有内容
			if (list != null) {
				for (FileItem item : list) {
					// 判断是否为普通表单参数
					if (item.isFormField()) {
						// 普通表单参数
						// 获取表单的name属性名称
						String fieldName = item.getFieldName();
						// 获取表单参数值
						String value = item.getString("utf-8");
						System.out.println("------"+fieldName+"-------" + value);
						if(fieldName.equals("chunks")){
							chunks=value;
						}
						if(fieldName.equals("chunk")){
							chunk=value;
						}
						if(fieldName.equals("fileMd5")){
							fileMd5=value;
						}
					} else {
						System.out.println("------chunks-------" + chunks);
						System.out.println("------fileMd5-------" + fileMd5);
						// 为每个文件创建一个目录，并保存这个文件的所有分块文件
						// 判断是否已经分块上传
						if (chunks != null) {
							System.out.println("分块处理...");
							// 进行分块上传了

							// 建立一个临时目录，用于保存所有分块文件
							File chunksDir = new File(serverPath + "/"+ fileMd5);
							if (!chunksDir.exists()) {
								chunksDir.mkdir();
							}

							if (chunk != null) {
								// 保存分块文件
								File chunkFile = new File(chunksDir.getPath()+ "/" + chunk);
								// 文件
								if (item.getName() != null && !item.getName().equals("")) {
									// 保存到服务器硬盘
									try {
										item.write(chunkFile);
									} catch (Exception e) {
										e.printStackTrace();
									}
									item.delete();
								}
							}
							
						}
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		response.getWriter().write("{\"filePath\":\" "+serverPath+" \"}");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
